package com.xzs.thinking.service.impl;

import com.xzs.thinking.dao.HelloWorldDao;
import com.xzs.thinking.dao.HelloWorldRedisDao;
import com.xzs.thinking.dao.es.HelloWorldEsDao;
import com.xzs.thinking.dao.mapper.dev.HelloWorldDevMapper;
import com.xzs.thinking.dao.mapper.uat.HelloWorldUatMapper;
import com.xzs.thinking.model.HelloWorldEsVO;
import com.xzs.thinking.model.HelloWorldVO;
import com.xzs.thinking.service.HelloWorldService;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author : Iris.Xiao
 * @date : 2017/11/17 16:46
 * @description :
 */
@Service("helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

    @Autowired
    private HelloWorldDao helloWorldDao;
    @Autowired
    private HelloWorldRedisDao helloWorldRedisDao;
    @Autowired
    private HelloWorldDevMapper helloWorldDevMapper;
    @Autowired
    private HelloWorldUatMapper helloWorldUatMapper;
    @Autowired
    private HelloWorldEsDao helloWorldEsDao;
    public void exceptionTest(){
        int i = 10/0;
    }

    public HelloWorldVO getVo(String id){
//        return helloWorldDao.getById(id);
        return helloWorldDao.getByIdDataSource2(id);
    }

    public HelloWorldVO getVoByMybatisDev(String id){
        return helloWorldDevMapper.findById(id);
    }
    public HelloWorldVO getVoByMybatisUat(String id){
        return helloWorldUatMapper.findById(id);
    }

    public void updateDev(HelloWorldVO helloWorldVO){
        helloWorldDevMapper.update(helloWorldVO);
        int i = 10/0;
    }

    public void updateUat(HelloWorldVO helloWorldVO){
        helloWorldUatMapper.update(helloWorldVO);
        int i = 10/0;
    }
    public void save(HelloWorldVO helloWorldVO){
        System.out.println("保存");
        helloWorldRedisDao.save(helloWorldVO);
    }
    public String get(String id){
        Map map = helloWorldRedisDao.get(id);
        if(map==null||map.size()==0){
            System.out.println("数据库取数据");
            HelloWorldVO helloWorldVO = this.getVo(id);
            if(helloWorldVO==null){
                System.out.println("无数据");
                return null;
            }
            return helloWorldVO.getNickName();
        }
        System.out.println("缓存取数据");
        return map.get("nickName").toString();
    }


    public static Random random =new Random();

    //任务一;
    @Async
    public void doTaskOne() throws Exception {
        System.out.println("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(3000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务一，耗时：" + (end - start) + "毫秒");
    }

    //任务二;
    @Async
    public void doTaskTwo() throws Exception {
        System.out.println("开始做任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(3000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务二，耗时：" + (end - start) + "毫秒");
    }

    //任务3;
    @Async
    public void doTaskThree() throws Exception {
        System.out.println("开始做任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(3000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务三，耗时：" + (end - start) + "毫秒");
    }


    @Override
    public HelloWorldEsVO saveEs(HelloWorldEsVO helloWorldEsVO) {
        helloWorldEsVO.setId(System.currentTimeMillis());
        HelloWorldEsVO result = helloWorldEsDao.save(helloWorldEsVO);
        return result;
    }

    @Override
    public List<HelloWorldEsVO> searchHelloWorldEsVO(Integer pageNumber,
                                                     Integer pageSize,
                                                     String searchContent) {
        // 分页参数
        Pageable pageable = new PageRequest(pageNumber, pageSize);

        // Function Score Query
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("title", searchContent)),
                        ScoreFunctionBuilders.weightFactorFunction(1000))
                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("content", searchContent)),
                        ScoreFunctionBuilders.weightFactorFunction(100));

        // 创建搜索 DSL 查询
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(functionScoreQueryBuilder).build();


        Page<HelloWorldEsVO> searchPageResults = helloWorldEsDao.search(searchQuery);
        return searchPageResults.getContent();
    }
}
